package com.fr.report.io;

import com.fr.base.Style;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.report.FineBook;
import com.fr.report.Report;
import com.fr.report.ResultWorkBook;
import com.fr.report.TemplateWorkBook;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.io.xml.SynchronizedStyleList;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

public class TemplateExporter extends AbstractEmbExporter
{
  public void export(OutputStream paramOutputStream, ResultWorkBook paramResultWorkBook)
    throws Exception
  {
    exportBook(paramOutputStream, new ResultBookRXML(paramResultWorkBook));
  }

  public void export(OutputStream paramOutputStream, TemplateWorkBook paramTemplateWorkBook)
    throws Exception
  {
    exportBook(paramOutputStream, new TemplateBookRXML(paramTemplateWorkBook));
  }

  private void exportBook(OutputStream paramOutputStream, RXML paramRXML)
    throws Exception
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    localByteArrayOutputStream.writeTo(paramOutputStream);
    export(paramRXML, paramOutputStream);
    localByteArrayOutputStream.flush();
    localByteArrayOutputStream.close();
  }

  private static void export(RXML paramRXML, OutputStream paramOutputStream)
    throws Exception
  {
    paramRXML.nullPointException();
    XMLPrintWriter localXMLPrintWriter = XMLPrintWriter.create(paramOutputStream);
    SynchronizedStyleList localSynchronizedStyleList = SynchronizedStyleList.getSynchronizedStyleList(Thread.currentThread());
    paramRXML.openTag(localXMLPrintWriter);
    paramRXML.mainContent(localXMLPrintWriter);
    localXMLPrintWriter.startTAG("StyleList");
    int i = localSynchronizedStyleList.getStyleCount();
    for (int j = 0; j < i; ++j)
    {
      Style localStyle = localSynchronizedStyleList.getStyle(j);
      if (localStyle != null)
        ReportXMLUtils.writeStyle(localXMLPrintWriter, localStyle);
    }
    localXMLPrintWriter.end();
    localXMLPrintWriter.end();
    localXMLPrintWriter.flush();
    localXMLPrintWriter.close();
    SynchronizedStyleList.removeSynchronizedCellStyleList(Thread.currentThread());
  }

  private static class ResultBookRXML extends TemplateExporter.TemplateBookRXML
  {
    ResultBookRXML(FineBook paramFineBook)
    {
      super(paramFineBook);
    }

    public void openTag(XMLPrintWriter paramXMLPrintWriter)
    {
      paramXMLPrintWriter.startTAG("RWorkBook");
    }
  }

  private static class TemplateBookRXML
    implements TemplateExporter.RXML
  {
    private FineBook b;

    TemplateBookRXML(FineBook paramFineBook)
    {
      this.b = paramFineBook;
    }

    public void nullPointException()
      throws NullPointerException
    {
      if (this.b == null)
        throw new NullPointerException("WorkBook cannot be null");
    }

    public void openTag(XMLPrintWriter paramXMLPrintWriter)
    {
      paramXMLPrintWriter.startTAG("WorkBook");
    }

    public void mainContent(XMLPrintWriter paramXMLPrintWriter)
    {
      this.b.writeXML(paramXMLPrintWriter);
    }
  }

  private static class ReportRXML
    implements TemplateExporter.RXML
  {
    private Report r;

    ReportRXML(Report paramReport)
    {
      this.r = paramReport;
    }

    public void nullPointException()
      throws NullPointerException
    {
      if (this.r == null)
        throw new NullPointerException("Report cannot be null");
    }

    public void openTag(XMLPrintWriter paramXMLPrintWriter)
    {
      paramXMLPrintWriter.startTAG("Report").attr("class", this.r.getClass().getName());
    }

    public void mainContent(XMLPrintWriter paramXMLPrintWriter)
    {
      this.r.writeXML(paramXMLPrintWriter);
    }
  }

  private static abstract interface RXML
  {
    public abstract void nullPointException();

    public abstract void openTag(XMLPrintWriter paramXMLPrintWriter);

    public abstract void mainContent(XMLPrintWriter paramXMLPrintWriter);
  }
}